/// <reference types = "chrome" />

// 在扩展程序启动时添加初始规则

chrome.runtime.onInstalled.addListener(() => {
  //定义规则
  const initialRules = [
    {
      id: 2,
      priority: 2,
      action: {
        type: "modifyHeaders", // 参考：https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-RuleActionType
        requestHeaders: [
          {
            header: "test",
            operation: "set", //还可以是 append remove 等
            value: "6666"
          }
        ]
      },
      condition: {
        urlFilter: "www.runoob.com", //域名
        resourceTypes: ["xmlhttprequest"] // 类别
      }
    }
  ]
  chrome.declarativeNetRequest.getDynamicRules(function (res) {
    let rules = res.map((e) => e.id)
    chrome.declarativeNetRequest.updateDynamicRules(
      {
        addRules: initialRules, //Rule[] optional
        removeRuleIds: rules //number[] optional
      } as chrome.declarativeNetRequest.UpdateRuleOptions,
      function (callback) {}
    )
  })
})
